﻿using MuseumApp315.Data;
using MuseumApp315.Data.Models;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MuseumApp315.Adapters;

namespace MuseumApp315.Controllers
{
    public class MuseumsController : ApiController
    {
        // Private vars...
        IMuseumAdapter _adapter;
        // Constuctor 
        public MuseumsController()
        {
            _adapter = new MuseumDataAdapter();
        }
        // overload for a specific adapter
        public MuseumsController(IMuseumAdapter adp)
        {
            _adapter = adp;
        }



        [Route("api/rickjames")]
        public IHttpActionResult Get()
        {
            var myArr = new String[] { "couch", "fire" };
            return Ok(myArr);
        }

        // Get all meuseums
        [Route("api/showmuseums")]
        public IHttpActionResult GetAll()
        {
            List<Museum> mList = new List<Museum>();

            using(ApplicationDbContext db = new ApplicationDbContext()){

                mList = db.Museums.Include(r => r.Reviews).ToList();

                var fResults = mList.Select(m => new
                {
                    MuseumId = m.MuseumId,
                    Name = m.Name,
                    City = m.City,
                    Description = m.Description,

                    //Tags = db.MuseumTags.Where(t => t.MuseumId == m.MuseumId).Select(t => new { Id = t.TagId, Name = t.Tag }).ToList(),
                    
                    Reviews = m.Reviews.Select(r => new
                    {
                        ReviewId = r.ReviewId,
                        UserName = r.UserName
                    })
                });
                return Ok(fResults);

            }
        }

        // get a specific Meuseum...
        [Route("api/muse/{id}")]
        public IHttpActionResult Get(int id)
        {


            List<Museum> mList = new List<Museum>();

            using (ApplicationDbContext db = new ApplicationDbContext())
            {

                mList = db.Museums.Where(m => m.MuseumId == id).Include(r => r.Reviews).ToList();

                var fResults = mList.Select(m => new
                {
                    MuseumId = m.MuseumId,
                    Name = m.Name,
                    City = m.City,
                    Description = m.Description,

                    Tags = db.MuseumTags.Where(t => t.MuseumId == id).Select(t => t.Tag).ToList(),

                    Reviews = m.Reviews.Select(r => new
                    {
                        ReviewId = r.ReviewId,
                        UserName = r.UserName,
                        Comment = r.Comment
                    })
                }).ToList();
                return Ok(fResults);

            }
        }

        // Search
        // get a specific Meuseum...
        [Route("api/search/{str}")]
        public IHttpActionResult Get(string str)
        {
            //
            List<Museum> mList = new List<Museum>();

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                 // SEARCH homework/task

                // ANY
                // WHERE
                // CONTAINS
                // INCLUDE
                // ....
                // Range (price, dates, count)
                // other
                mList = db.Museums.Where(n => n.City.Contains(str) || n.Description.Contains(str)).Include(r => r.Reviews).ToList();

                var fResults = mList.Select(m => new
                {
                    MuseumId = m.MuseumId,
                    Name = m.Name,
                    City = m.City,
                    Description = m.Description,

                    //Tags = db.MuseumTags.Where(t => t.MuseumId == m.MuseumId).Select(t => new { Id = t.TagId, Name = t.Tag }).ToList(),

                    Reviews = m.Reviews.Select(r => new
                    {
                        ReviewId = r.ReviewId,
                        UserName = r.UserName
                    })
                });
                return Ok(fResults);

            }

        }



    }
}
